<?php
namespace App\Api\Controllers\EasyPay;


use App\Api\Controllers\Config\WeixinConfigController;
use App\Models\WeixinNotify;
use Dingo\Api\Http\Request;
use Faker\Factory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

final class TestController
{
    private $payment;

    public function __construct()
    {
        $this->url = 'https://notify-test.eycard.cn:7443/WorthTech_Access_AppPaySystemV2/apppayacc';
        $this->payment = new Payment($this->url);
    }


    //更新并获取签名密钥
    public function test11()
    {
        $params = [
            "channelid" => "D01000000089888", //渠道编号
            "opt" => "getSign", //操作类型
        ];

        $key = "d5riddkald4k4did"; //渠道密钥

        $result = $this->payment->request($params, $key);
        print_r($result);die;
//        {"opt":"getSign","sign":"AEFFB0BA1D2C68C99FBD140469C72A76","resultcode":"00","key":"8od1d4o2i0dkj7lk212dkild7adi275l","channelid":"D01000000089888"}
    }


    //用户被扫，商户扫用户码
    public function testMechantScanPayerCodePayment()
    {
        $params = [
            "channelid" => "D01000000089888",
            "merid" => "531000012966395",
            "termid" => "W1486800",
            "opt" => "scanPay",
            "tradetrace" => "123343334",
            "tradeamt" => "1",
            "authcode" => "134782163690476394",
            "body" => "sss",
            "version" => "", // 版本号-非必填
            "handingfee" => "" // 手续费-非必填
        ];

        $key = "8od1d4o2i0dkj7lk212dkild7adi275l";
//        $key = "7dlddsvod8la50jls3js3klda31ddr8d";

        $result = $this->payment->request($params, $key);
        print_r($result);die;
//        {"termid":"W1486800","opt":"scanPay","tradetrace":"123343334","returnmsg":"101付款码无效，请重新扫码","wtorderid":"11420200703094243115143","sign":"A42BBDBD8920FBC1CBE2BDB829E1A1A7","merid":"531000012966395","resultcode":"06","channelid":"D01000000089888"}
//        $this->assertNotEmpty($result);
//        $this->assertContains("123343334", $result);
    }


    //用户主扫，用户扫商户码
    public function testPayerScanMechantCodePayment()
    {
        $params = [
            "channelid" => "D01000000089888",
            "merid" => "531000012966395",
            "termid" => "W1486800",
            "opt" => "apPreOrder",
            "tradetype" => "NATIVE",
            "tradetrace" => "alipay_scan2020070356789",
            "tradeamt" => "1",
            "body" => "sss",
            "notifyurl" => url('api/easypay/pay_notify_url'),
            "version" => "",// 版本号-非必填
            "handingfee" => ""// 手续费-非必填
        ];

        $key = "8od1d4o2i0dkj7lk212dkild7adi275l";

        $result = $this->payment->request($params, $key);
        print_r($result);die;
//        {"codeurl":"https://qr.alipay.com/bax01591e1t2xjq4wuld2073","tradetype":"NATIVE","termid":"W1486800","opt":"apPreOrder","tradetrace":"alipay_scan2020070356789","returnmsg":"Success","wtorderid":"21520200703095003115147","sign":"8928AE93A4435F7373700273C0938695","merid":"531000012966395","prepayid":"","resultcode":"00","channelid":"D01000000089888"}
//        $this->assertNotEmpty($result);
//        $this->assertContains("codeurl", $result);
//        $this->assertContains("apPreOrder", $result);
//        $this->assertContains("1233433345", $result);
    }


    //订单查询
    public function testPaymentQuery()
    {
        $params = [
            "channelid" => "D01000000089888",
            "merid" => "531000012966395",
            "termid" => "W1486800",
            "opt" => "tradeQuery",
            "tradetrace" => "alipay_scan2020070356789"
        ];

        $key = "8od1d4o2i0dkj7lk212dkild7adi275l";

        $result = $this->payment->request($params, $key);
        print_r($result);die;
//        {"termid":"W1486800","opt":"tradeQuery","tradetrace":"alipay_scan2020070356789","returnmsg":"订单未支付","wtorderid":"21520200703095003115147","sign":"B035D513BCDB78D43B45C17849304D4C","merid":"531000012966395","resultcode":"AA","channelid":"D01000000089888"}
//        $this->assertNotEmpty($result);
//        $this->assertContains("123343334", $result);
    }


    //退款
    public function returnOrder()
//        public function test()
    {
        $params = [
            "channelid" => "D01000000089888", //渠道编号
            "merid" => "531000012966395", //商戶编号
            "termid" => "W1486800", //终端编号
            "tradetrace" => "alipay_scan2020070356789", //商户订单号
            "opt" => "zwrefund", //操作类型（zwrefund）
//            "oritradetrace" => "", //原交易流水； 与 oriwtorderid 字段可以二选一上送，如果都上送，以本节点为准
            "oriwtorderid" => "21520200703095003115147", //原交易的系统订单号；与 oritradetrace 字段可以二选一上送，如果都上送，以 oritradetrace 节点为准
            "tradeamt" => 1, //退货金额,以分为单位，不大于原交易金额与已成功退货金额之差
        ];

//        $key = "8od1d4o2i0dkj7lk212dkild7adi275l";
        $key = "i15d5k4k7jovod2dl20af1das5k2dsli";

        $result = $this->payment->request($params, $key);
        print_r($result);die;
//        {"termid":"W1486800","opt":"tradeQuery","tradetrace":"alipay_scan2020070356789","returnmsg":"验证签名失败","merid":"531000012966395","resultcode":"A0","channelid":"D01000000089888"}
//        {"termid":"W1486800","opt":"zwrefund","tradetrace":"alipay_scan2020070356789","returnmsg":"请求参数不合法@ oritradetrace or oriwtorderid","merid":"531000012966395","resultcode":"01","channelid":"D01000000089888"}
//        {"termid":"W1486800","opt":"zwrefund","tradetrace":"alipay_scan2020070356789","returnmsg":"原交易结果未确定交易,请先对原交易进行查询操作","sign":"939977D052F281396BB2989DEAC6C732","merid":"531000012966395","resultcode":"08","channelid":"D01000000089888"}
    }


    public function tearDown() {
        unset($this->url);
        unset($this->payment);
    }


}
